package engine.data
{
	import engine.net.datatype.ByteArrayEx;
	
	import flash.events.IEventDispatcher;
	
	import newx.BaseObject;
	import newx.utils.debug.IO;

	
	public class ObjectData extends BaseObject
	{	
		protected	var		_objKey:String;	
		
		//父容器
		protected	var		_parent			:BaseObject;
		protected	var		_bindManage		:BindManage;	
		
		public	static var ProertyNames	:Array		=	[];
		
		public	static var ProertyTypes	:Array		=	[];
		
		public function ObjectData(parent:ObjectData,objKey:String,target:IEventDispatcher=null)
		{		
			super(target);
			_parent		=	parent;
			_objKey		=	objKey;
			_bindManage	=	BindManage.getInstance();
		}
		
		public function get parent():BaseObject
		{
			return _parent;
		}
		public function set parent(val:BaseObject):void
		{
			_parent	=	val;
		}

		public function GetPropertiesEx(PropertiesName:String):*
		{	
			if(!CheckProperties(PropertiesName))
			{
				IO.traceError("属性 = " + PropertiesName +　"       " + "不存在 ------------------------------------>"  );
				return 0;
			}
			return super.GetProperties(PropertiesName);			
		}
		
		public function DeletePropertiesEx(PropertiesName:String):void
		{
			if(CheckProperties(PropertiesName))
			{
				var	PropertiesValue:*	=	GetProperties(PropertiesName);			
				super.DeleteProperties(PropertiesName);
				_bindManage.CallBind(this,PropertiesName,PropertiesValue,false);	
				
				if(PropertiesValue is ObjectData)
				{
					//假如对象移除，移除该对象所有绑定
					_bindManage.UnbindPropAll(PropertiesValue,PropertiesName);
				}
			}
		}
		/**
		 * 	根据数据流缓存本地数据
		 * @param count		服务端属性个数
		 * @param msg		服务端属性数据
		 * 
		 */
		public function setPropertiesFormByteArrayEx(count:int, msg:ByteArrayEx):void
		{
			var i:uint = 0;			
			for (i = 0; i < count; i++)
			{ 
				var index:uint		= msg.readUnsignedShort();
				var proName:String	= ProertyNames[index];
				var	data:*			= msg.GetDataByType(ProertyTypes[index]);
				HandlePropertiesEx(proName,data);
			}
		}
		
		public function UpdataPropertiesEx(PropertiesName:String, PropertiesValue:*):void
		{
			super.HandleProperties(PropertiesName,PropertiesValue);	
			_bindManage.CallView(this as View,PropertiesName,PropertiesValue,true,true);

		}
		
		/**
		 * 重新ObjectData的方法，添加服务器Addobj时回调的处理 
		 * @param PropertiesName
		 * @param PropertiesValue
		 * 
		 */		
		public function HandlePropertiesEx(PropertiesName:String, PropertiesValue:*):void
		{
			super.HandleProperties(PropertiesName,PropertiesValue);	
			_bindManage.CallBind(this,PropertiesName,PropertiesValue,true);
		}
		
		public	function	get ObjKey():String
		{
			return _objKey;
		}
		override public function toString():String
		{
			var str:String=super.toString()+"\n{\n";
			for (var i:int = 0; i < ProertyNames.length; i++) 
			{
				if(CheckProperties(ProertyNames[i])&&GetProperties(ProertyNames[i]))
				str+=(ProertyNames[i]+":"+GetProperties(ProertyNames[i])+",\n")
			}
			return str+"\n}\n";			
		}
		 public function toString2():String
		{
			var str:String="toString2=>"+super.toString();
			for  (var i:String in Properties) 
			{
				str+=(i+":"+Properties[i]+",\n")
			}
			
			return str;			
		}
		 
		 static public function fromObj(obj:Object):ObjectData
		 {
			 var baseObj:ObjectData	=	new ObjectData( null, "" );
			 for(var proName:String	in 	obj)
			 {
				 baseObj.HandleProperties(proName,obj[proName]);
			 }
			 return baseObj;
		 }
	}
}